//
//  NSDate+Str.m
//  SecondPackage
//
//  Created by 韩艳锋 on 2019/7/5.
//  Copyright © 2019 韩艳锋. All rights reserved.
//

#import "NSDate+Str.h"

@implementation NSDate (Str)

-(NSString*)year{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];
    return [formatter stringFromDate:self];
}
-(NSString*)month{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MM"];
    return [formatter stringFromDate:self];
}
-(NSString*)day {
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd"];
    return [formatter stringFromDate:self];
}
-(NSString*)hour{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH"];
    return [formatter stringFromDate:self];
}
-(NSString*)minute{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"mm"];
    return [formatter stringFromDate:self];
}
-(NSString*)second{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"ss"];
    return [formatter stringFromDate:self];
}

-(NSString*)getFormateOne {
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    return [formatter stringFromDate:self];
}
-(NSString*)getFormateTow {
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    return [formatter stringFromDate:self];
}

+(instancetype)initWithStringFormat:(NSString*)string {
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
//      [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//解决8小时时间差问题
     NSDate *birthdayDate = [dateFormatter dateFromString:string];
    return birthdayDate;
}

- (NSDate *)getNewDateWithDay:(int)day
                  dayRelative:(BOOL)dayRelative
                         hour:(int)hour
                 hourRelative:(BOOL)hourRelative
                       minute:(int)minute
               minuteRelative:(BOOL)minuteRelative {
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:self];
    if (dayRelative) {
        if (day != 0) {
            [components setDay:([components day]+day)];
        }
    } else {
        [components setDay:(day)];
    }
    if (hourRelative) {
        if (hour != 0) {
            [components setHour:[components hour] + hour];
        }
    } else {
        [components setHour:hour];
    }
    if (minuteRelative) {
        if (minute != 0) {
            [components setMinute:[components minute] + minute];
        }
    } else {
        [components setMinute:minute];
    }
    return [gregorian dateFromComponents:components];
}
@end
